package cn.likole.oj.module;


import cn.likole.oj.bean.Family;
import cn.likole.oj.bean.Familylink;
import cn.likole.oj.bean.User;
import cn.likole.oj.bean.UserInfo;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;
import org.nutz.plugins.apidoc.annotation.Api;

import javax.servlet.http.HttpSession;
import java.util.List;

@IocBean
@At("/family")
@Filters(@By(type = CheckSession.class, args = {"uid", "/"}))
@Api(name = "家族模块")
public class FamilyModule {

    @Inject
    private Dao dao;


    @At
    public Object createFamily(HttpSession session,@Param("familyname")String fname){
        NutMap re=new NutMap();
        UserInfo userInfo=dao.fetch(UserInfo.class,(int)session.getAttribute("uid"));
        if(userInfo==null)
            return re.setv("ok",false).setv("msg","没有该江湖人士或未下山");
        if(userInfo.getFamilyId()!=0)
            return re.setv("ok",false).setv("msg","该江湖人士已有队伍");
        Family family=new Family();
        family.setFamilyName(fname);
        family.setMemNum(1);
        family.setPatriarchId(userInfo.getUserId());
        family.setSolved(0);
        family.setSubmit(0);
        family=dao.insert(family);
        userInfo.setFamilyId(family.getFamilyId());
        userInfo.setPatriarch(true);
        int rows1=dao.update(userInfo);
        List<Familylink> familylinks=dao.query(Familylink.class,Cnd.where("userId","=",userInfo.getUserId()));
        if(familylinks!=null)
            dao.delete(familylinks);
        if(rows1==0)
            return re.setv("ok",false).setv("msg","数据库更新错误");
        return re.setv("ok",true);
    }


    @At
    public List<User> getPatList(HttpSession session){
        String sqlstr="SELECT * from user where userId in (select userId from user_info where familyId=0) and userId!=@userId LIMIT 0,50";
        Sql sql= Sqls.create(sqlstr);
        sql.setParam("userId",session.getAttribute("uid"));
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(User.class));
        dao.execute(sql);
        return sql.getList(User.class);
    }

    @At
    @Api(name = "获取家族信息",description = "获取家族信息列表,无参数")
    public List<Family> getFamilyList(HttpSession session){
        return dao.query(Family.class,Cnd.NEW());
    }



    @At
    @Api(name = "申请入队",description = "申请加入队伍，参数队伍id：familyId")
    public Object askJoin(HttpSession session,@Param("familyId")int familyId){
        UserInfo userInfo=dao.fetch(UserInfo.class,(int)session.getAttribute("uid"));
        if(userInfo.getFamilyId()!=0)
            return new NutMap().setv("ok",false).setv("msg","已有队伍");
        Familylink familylink=dao.fetchx(Familylink.class,familyId,userInfo.getUserId());
        if(familylink!=null)
            return new NutMap().setv("ok",false).setv("msg","该队伍已经邀请你");
        familylink=new Familylink();
        familylink.setFamilyId(familyId);
        familylink.setUserId((int)session.getAttribute("uid"));
        familylink.setState(false);
        dao.insert(familylink);
        return new NutMap().setv("ok",true);
    }


    @At
    @Api(name = "邀请用户入队",description = "邀请用户入队,队伍familyId,用户userId")
    public Object inviteMem(@Param("familyId")int familyId,@Param("userId")int userId){
        Family family=dao.fetch(Family.class,familyId);
        Familylink familylink=dao.fetchx(Familylink.class,familyId,userId);
        if(familylink!=null)
            return new NutMap().setv("ok",false).setv("msg","该成员已经提交申请");
        familylink=new Familylink();
        familylink.setFamilyId(familyId);
        familylink.setUserId(userId);
        familylink.setState(true);
        dao.insert(familylink);
        return new NutMap().setv("ok",true);
    }


    @At
    @Api(name = "获取队伍请求",description = "获取未处理的familylink(请求)")
    public List<Familylink> getReqInfo(HttpSession session){
        UserInfo userInfo=dao.fetch(UserInfo.class,(int)session.getAttribute("uid"));
        if(userInfo.isPatriarch())
            return dao.query(Familylink.class,Cnd.where("familyId","=",userInfo.getFamilyId()).and("state","=",false));
        return dao.query(Familylink.class,Cnd.where("userId","=",session.getAttribute("uid")).and("state","=",true));
    }


    @At
    @Api(name = "同意入队",description = "同意某用户的入队申请,队伍familyId,用户userId")
    public Object acceptMem(@Param("familyId")int familyId,@Param("userId")int userId){
        NutMap re=new NutMap();
        Family family=dao.fetch(Family.class,familyId);
        family.setMemNum(family.getMemNum()+1);
        int rows1=dao.update(family);
        UserInfo userInfo=dao.fetch(UserInfo.class,userId);
        userInfo.setFamilyId(familyId);
        int rows2=dao.update(userInfo);
        Familylink familylink=dao.fetchx(Familylink.class,familyId,userId);
        int rows3=dao.delete(familylink);
        if(rows1==0||rows2==0||rows3==0)
            return re.setv("ok",false).setv("msg","数据库更新错误");

        return re.setv("ok",true);
    }

    @At
    @Api(name = "拒绝入队",description = "拒绝用户入队申请,队伍familyId,用户userId")
    public Object rejectMem(@Param("familyId")int familyId,@Param("userId")int userId){
        Familylink familylink=dao.fetch(Familylink.class,Cnd.where("familyId","=",familyId).and("userId","=",userId));
        int rows=dao.delete(familylink);
        if(rows==0)
            return new NutMap().setv("ok",false).setv("msg","数据库更新错误");
        return new NutMap().setv("ok",true);
    }




    @At
    @Api(name = "接受邀请",description = "接受邀请,队伍familyId")
    public Object acceptInvite(@Param("familyId")int familyId,HttpSession session){
        //check member number
        Family family=dao.fetch(Family.class,familyId);
        //update userInfo
        UserInfo userInfo=dao.fetch(UserInfo.class,(int)session.getAttribute("uid"));
        userInfo.setFamilyId(familyId);
        int rows1=dao.update(userInfo);
        //update familyInfo
        family.setMemNum(family.getMemNum()+1);
        int rows2=dao.update(family);
        Familylink familylink=dao.fetchx(Familylink.class,familyId,session.getAttribute("uid"));
        dao.delete(familylink);
        if(rows1==0||rows2==0)
            return new NutMap().setv("ok",false).setv("msg","数据库更新错误");
        return new NutMap().setv("ok",true);
    }


    @At
    @Api(name = "拒绝邀请",description = "拒绝邀请,队伍familyId,用户userId")
    public Object rejectInvite(@Param("familyId")int familyId,@Param("userId")int userId){
        Familylink familylink=dao.fetchx(Familylink.class,familyId,userId);
        int rows=dao.delete(familylink);
        if(rows==0)
            return new NutMap().setv("ok",false).setv("msg","数据库更新错误");
        return new NutMap().setv("ok",true);
    }


    @At
    public Object dissolve(HttpSession session){
        NutMap re=new NutMap();
        UserInfo userInfo=dao.fetch(UserInfo.class,(int)session.getAttribute("uid"));
        Family family=dao.fetch(Family.class,userInfo.getFamilyId());
        int rows1=dao.delete(family);
        int rows2=dao.update(UserInfo.class, Chain.make("tid",0).add("isCap",false),Cnd.where("tid","=",family.getFamilyId()));
        if(rows1==0||rows2==0)
            return re.setv("ok",false).setv("msg","数据库更新错误");
        return re.setv("ok",true);
    }


}
